package com.wgf.controller;

import com.wgf.service.ChatAssistant;
import dev.langchain4j.model.chat.StreamingChatModel;
import dev.langchain4j.model.chat.response.ChatResponse;
import dev.langchain4j.model.chat.response.StreamingChatResponseHandler;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;

@RestController
@Slf4j
public class StreamingChatModelController
{
    @Resource //直接使用 low-level LLM API
    private StreamingChatModel streamingChatLanguageModel;//直接使用LangChain4j的低级API
    @Resource //自己封装接口使用 high-level LLM API
    private ChatAssistant chatAssistant;//使用自定义封装的高级API


    // http://localhost:9007/chatstream/chat?prompt=天津有什么好吃的
    @GetMapping(value = "/chatstream/chat")
    public Flux<String> chat(@RequestParam("prompt") String prompt)//返回Flux<String>类型，用于流式响应
    {
        return Flux.create(stringFluxSink -> {//创建Flux流，使用低级API直接处理流式响应
            streamingChatLanguageModel.chat(prompt, new StreamingChatResponseHandler()//实现StreamingChatResponseHandler的三个回调方法
            {
                @Override//onPartialResponse: 接收到部分响应时，通过Flux发送给客户端
                public void onPartialResponse(String s)
                {
                    stringFluxSink.next(s);
                }

                @Override//onCompleteResponse: 响应完成时，关闭Flux流
                public void onCompleteResponse(ChatResponse completeResponse)
                {
                    stringFluxSink.complete();
                }

                @Override//onError: 发生错误时，通过Flux发送错误信息
                public void onError(Throwable throwable)
                {
                    stringFluxSink.error(throwable);
                }
            });
        });
    }

    @GetMapping(value = "/chatstream/chat2")//使用低级API处理流式响应，但将所有响应打印到控制台而不是返回给客户端,主要用于调试和测试目的
    public void chat2(@RequestParam(value = "prompt", defaultValue = "北京有什么好吃") String prompt)
    {
        System.out.println("---come in chat2");
        streamingChatLanguageModel.chat(prompt, new StreamingChatResponseHandler()
        {
            @Override
            public void onPartialResponse(String partialResponse)
            {
                System.out.println(partialResponse);
            }

            @Override
            public void onCompleteResponse(ChatResponse completeResponse)
            {
                System.out.println("---response over: "+completeResponse);
            }

            @Override
            public void onError(Throwable throwable)
            {
                throwable.printStackTrace();
            }
        });
    }



    @GetMapping(value = "/chatstream/chat3")
    public Flux<String> chat3(@RequestParam(value = "prompt", defaultValue = "南京有什么好吃") String prompt)
    {
        System.out.println("---come in chat3");

        return chatAssistant.chatFlux(prompt);//使用自定义的ChatAssistant服务处理请求,返回服务生成的Flux流，实现了业务逻辑与控制器分离
    }
}
